<%@include file="_header.jspf" %>
<%@page import="com.spartancoder.accommodation.beans.PlaceBean" %>
<%@page import="com.spartancoder.accommodation.models.Place" %>
<%
    String error = null;

    String name = request.getParameter("name");
    String address = request.getParameter("address");

    int rooms = (request.getParameter("rooms") == null)?0:Integer.parseInt(request.getParameter("rooms"));
    int placesPerRoom = (request.getParameter("placesPerRoom") == null)?0:Integer.parseInt(request.getParameter("placesPerRoom"));

    if( name != null && address != null ) {
        if(name.trim().isEmpty() ) {
            error = "Trebuie sa completati un nume";
        } else if(rooms == 0) {
            error = "Trebuie sa completati un numar de camere";
        } else if(placesPerRoom == 0 ) {
            error = "Trebuie sa completati un numar de locuri per camera";
        } else {
            PlaceBean pb = new PlaceBean();
            pb.setName(name);
            pb.setAddress(address);
            pb.setRooms(rooms);
            pb.setPlacesPerRoom(placesPerRoom);
            pb.setAvailable(true);
            Place.create(pb);

            session.setAttribute("flash", "Caminul a fost adaugat");
            response.sendRedirect("list-place.jsp");
        }
    } else {
        name = "";
        address = "";
    }
%>


<h2>Adauga camin</h2>

<form action="add-place.jsp" method="post" class="span-12">
    <fieldset>
        <% if(error != null) { %>
        <br /><div class="error"><%=error%></div>
        <% } %>
        
        <label class="text large">Nume:</label><br />
        <input type="text" name="name" class="text large" value="<%=name%>" /><br />

        <label class="text large">Adresa:</label><br />
        <textarea name="address" class="text large" style="height: 50px;"><%=address%></textarea>
        
        <label class="text large">Numar camere:</label><br />
        <input type="text" name="rooms" class="text large" value="<%=rooms%>" /><br />

        <label class="text large">Locuri in camera:</label><br />
        <input type="text" name="placesPerRoom" class="text large" value="<%=placesPerRoom%>" /><br />

        <button type="submit" class="button positive">
            Adauga camin
        </button>

    </fieldset>
</form>


<%@include file="_footer.jspf" %>